package com.sky.controller.admin;


import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.EmployeeServiceImpll;
import com.sky.utils.JwtUtill;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
public class EmployeeControllerr {
    @Autowired
    private EmployeeServiceImpll employeeServiceImpll;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login( @RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("员工登录:{}",employeeLoginDTO);

        Employee employee = employeeServiceImpll.login(employeeLoginDTO);

        // 登录成功，生成jwt
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtill.creatJwt(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        EmployeeLoginVO employeeLoginVO=new EmployeeLoginVO();
        employeeLoginVO.setId(employee.getId());
        employeeLoginVO.setName(employee.getName());
        employeeLoginVO.setUserName(employee.getUsername());
        employeeLoginVO.setToken(token);

//        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
//               .id(employee.getId())
//               .userName(employee.getUsername())
//               .name(employee.getName())
//               .token(token).build();

        return Result.success(employeeLoginVO);

    }

    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout(){
        return Result.success();
    }

    /**
     * 新增员工
     */
   @PostMapping
   @ApiOperation("新增员工")
   public Result save(@RequestBody EmployeeDTO employeeDTO){
       employeeServiceImpll.save(employeeDTO);
       return Result.success();
   }

    /**
     * 员工分页查询
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询：{}",employeePageQueryDTO);
        PageResult pageResult =employeeServiceImpll.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用、禁用员工账号
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用员工账号")
    public Result enable(@PathVariable ("status")Integer status,long id){
        log.info("启用禁用员工状态：{},{}",status,id);
        employeeServiceImpll.enable(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> getById(@PathVariable long id){
        log.info("根据id查询员工信息：{}",id);
        Employee employee=employeeServiceImpll.getById(id);
        return Result.success(employee);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工信息：{}",employeeDTO);
        employeeServiceImpll.update(employeeDTO);
        return Result.success();
    }



}
